using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using umbraco;
using umbraco.BusinessLogic;
using System.Collections.Generic;
using umbraco.BasePages;
using umbraco.BusinessLogic.Actions;
using umbraco.interfaces;
using umbraco.cms.presentation.Trees;
using System.Xml.XPath;

namespace umbraco.cms.presentation.user
{
    public partial class PermissionEditor : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Authorize();

            if (string.IsNullOrEmpty(Request.QueryString["id"]))
                return;

            CheckUser(Request.QueryString["id"]);

            hidUserID.Value = Request.QueryString["id"];
            
            ImageButton save = pnlUmbraco.Menu.NewImageButton();
            save.ID = "btnSave";
            save.ImageUrl = GlobalSettings.Path + "/images/editor/save.gif";
            save.OnClientClick = string.Format("return BeginSavePermissions();");

            nodePermissions.UserID = Convert.ToInt32(Request.QueryString["id"]);
            nodePermissions.NodeID = -1;

        }

        /// <summary>
        /// Since Umbraco stores users in cache, we'll use this method to retreive our user object by the selected id
        /// </summary>
        protected umbraco.BusinessLogic.User UmbracoUser
        {
            get
            {
                return BusinessLogic.User.GetUser(Convert.ToInt32(Request.QueryString["id"]));
            }
        }
      
        /// <summary>
        /// Makes sure the user exists with the id specified
        /// </summary>
        /// <param name="strID"></param>
        private void CheckUser(string strID)
        {
            int id;
            bool parsed = false;
            umbraco.BusinessLogic.User oUser = null;
            if (parsed = int.TryParse(strID, out id))
                oUser = umbraco.BusinessLogic.User.GetUser(id);

            if (oUser == null || oUser.UserType == null || !parsed)
                throw new Exception("No user found with id: " + strID);
        }

        private void Authorize()
        {
            if (!BasePage.ValidateUserContextID(BasePage.umbracoUserContextID))
                Response.Redirect(GlobalSettings.Path + "/logout.aspx?");

        }

    }
}
